package org.jeecg.modules.cascv.image.tools;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.HexUtil;
import com.alibaba.fastjson.JSONObject;

import java.io.RandomAccessFile;
import java.util.concurrent.TimeUnit;

/**
 * @author;作者
 * @Date 2023/3/18 10:15
 * @Description :
 * @Version
 */
public class BianzhongliDataProcess {

    public static void main(String[] args) throws Exception {
        RandomAccessFile randomAccessFile = new RandomAccessFile("E:\\11图像数据\\cut\\变重力\\变重力\\" +
                "TGWT_VGER_14V1_SCI_20230425161438_20230425161513_51_0_L0_V1.dat", "r");
        byte[] bytes = new byte[2024];

        while(true){
            int read = randomAccessFile.read(bytes);
            if(read <= 0){
                break;
            }

            String str = HexUtil.encodeHexStr(bytes);

            Integer startAndPackageNo = Integer.parseUnsignedInt(str.substring(12, 16), 16);
            // 1111 1111 1111 1111
            // 0011 1111 1111 1111
            System.out.println("startAndPackageNo = " + startAndPackageNo);

            int startFlag = startAndPackageNo >>> 14;

            int packageNo = startAndPackageNo & 0x3fff;

            System.out.println("startAndPackageNo = " + startAndPackageNo);

            String startFlagStr = Integer.toBinaryString(startFlag);
            if(startFlag==1||startFlag==0){
                startFlagStr="0"+startFlagStr;
            }
            Long second = Long.parseUnsignedLong(str.substring(16, 22), 16);

            Long subSecond = Long.parseUnsignedLong(str.substring(22, 26), 16);
            String frameStr = str.substring(32, 4042);

            System.out.println(second + "," + subSecond + "," + startFlag + "," + packageNo);
            String time = toDayHoursMinSec(second);
            System.out.println("time = " + time);

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("tableName", "21H_6D3DH");
            JSONObject content = new JSONObject();
            jsonObject.put("second",second);
            jsonObject.put("subsecond",subSecond);

            content.put("ETH_CCD1_KLB_CCD_HEAD_6_7_2",packageNo+";"+packageNo+";包序号");//序列标志
            content.put("ETH_CCD1_KLB_CCD_HEAD_8_11_1", second+";"+time+";时间码秒计数");//秒值
            content.put("ETH_CCD1_KLB_CCD_HEAD_12_13_1",subSecond+";"+subSecond+";时间码微秒计数");//微秒值
            content.put("ETH_CCD1_KLB_CCD_HEAD_6_7_1",startFlagStr+"B;"+startFlagStr+"B;序列标识");//序列标志
            content.put("ETH_CCD1_KLB_CCD_DATA_0_2005_1", "null;"+"0x"+frameStr+";null;null");//数据帧
            jsonObject.put("Content", content);


            FileUtil.appendUtf8String(jsonObject.toString() + System.lineSeparator(),
                    "E:\\11图像数据\\cut\\变重力\\变重力\\"+
                            "21H_6D3DH_1.h264");
            //封装json数据
            //推送kafka数据
        }
    }
    /**
     * 秒数转天时分秒
     */
    public static String toDayHoursMinSec(Long time){
        long day = TimeUnit.SECONDS.toDays(time);
        long hours = TimeUnit.SECONDS.toHours(time) - TimeUnit.DAYS.toHours(TimeUnit.SECONDS.toDays(time));
        long minutes = TimeUnit.SECONDS.toMinutes(time) - TimeUnit.HOURS.toMinutes(TimeUnit.SECONDS.toHours(time));
        long seconds = TimeUnit.SECONDS.toSeconds(time) - TimeUnit.MINUTES.toSeconds(TimeUnit.SECONDS.toMinutes(time));
        // 172800+14400+1720
        return day+":"+hours+":"+minutes+":"+seconds;
    }
}
